using System;
using System.Collections.Generic;
using UnityEngine;

namespace ai_library
{
	public class SendMessageToRandomAgentAction<T> : IAction<T> where T : IEntity
	{
		private MessageTypes m_messageType;

		private int m_delay;

		public SendMessageToRandomAgentAction (MessageTypes messageType, int delay)
		{
			m_messageType = messageType;
			m_delay = delay;

			Action = SendMessage;
		}

		public SendMessageToRandomAgentAction (MessageTypes messageType)
			: this (messageType, 0)
		{
		}

		public Action<T> Action
		{
			get;
			private set;
		}

		private void SendMessage(T t)
		{
			try
			{
				int receiverId = EntityManager.Instance.FindRandomAgent(t.ID);
				Message message = new Message(t.ID, receiverId, m_messageType, m_delay);
				MessageDispatcher.Instance.SendMessage(message);
			}
			catch(MessageDispatcherException e)
			{
				throw new ActionException(e.ToString());
			}
		}
	}
}

